package com.example.arithmeticleetcode.leetcode.dec;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-17 09:35
 **/
public class Demo15 {

    public static void main(String[] args) {
        int[] nums = {12};
        int[] nums1 = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(sumFourDivisors(nums));
        System.out.println(sumFourDivisors(nums1));
    }

    public static int sumFourDivisors(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            if (num < 6) continue;
            int count = 2;
            int tempSum = 1 + num;
            int midValue = num >> 1;
            int beginValue = 1;
            boolean flag = true;
            while (midValue > 1 && beginValue < midValue) {
                if ((num % midValue) == 0) {
                    if (!flag) {
                        count-=2;
                        break;
                    }
                    flag = false;

                    beginValue = num/midValue;
                    if (midValue == beginValue) {
                        count--;
                        tempSum += (midValue + beginValue);
                    } else {
                        tempSum += (midValue + beginValue);
                        count -= 2;
                    }
                    midValue--;
                } else {
                    midValue--;
                }
            }
            if (count == 0) {
                sum += tempSum;
            }
        }
        return sum;
    }
}
